package com.cyz.admin.controller;

import com.cyz.common.dto.CardDto;
import com.cyz.admin.service.CardService;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/card")
@Api(tags = "管理端课程相关接口")
public class CardController {

    @Autowired
    private CardService cardService;

    @PreAuthorize("@ps.hasPermission('contant:free:card')")
    @ApiOperation(value = "查询免费课程")
    @GetMapping("/queryCards")
    public R queryCards() {
        return cardService.queryCards();
    }

    @PreAuthorize("@ps.hasPermission('contant:free:card')")
    @ApiOperation(value = "查询免费课程以及按钮")
    @GetMapping("/queryCardsAndButtons")
    public R queryCardsAndButtons() {
        return cardService.queryCardsAndButtons();
    }

    @PreAuthorize("@ps.hasPermission('contant:free:card')")
    @ApiOperation(value = "更新免费课程")
    @PutMapping("/updateCard")
    public R updateFreeCard(@Validated(UpdateGroup.class) @RequestBody CardDto cardDto) {
        return cardService.updateCard(cardDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:card')")
    @ApiOperation(value = "增加免费课程")
    @PostMapping("/addCard")
    public R addCard(@Validated(AddGroup.class) @RequestBody CardDto cardDto) {
        return cardService.addCard(cardDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:card')")
    @ApiOperation(value = "删除免费课程")
    @DeleteMapping("/deleteCards")
    public R deleteCards(Integer[] ids) {
        return cardService.deleteCards(ids);
    }

}
